﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace DataScrubberExt
{
    public class GenericScrubber
    {
        private string  inputFile, outputFile;
        string[] xpaths;

        public GenericScrubber(string inputFile, string outputFile, string[] xpaths)
        {
            this.inputFile = inputFile;
            this.outputFile = outputFile;
            this.xpaths = xpaths;
        }
        public Boolean Scrubber()
        {
            XmlDocument xDoc = new XmlDocument();
            string inputFolderName = inputFile;
            string outputFolderName = outputFile;

            DirectoryInfo input = new DirectoryInfo(inputFolderName);
            FileInfo[] inputFileNameList = input.GetFiles();

            if (inputFileNameList.Count() != 0) { 

                foreach (FileInfo inputFileName in inputFileNameList)
                {
                    xDoc.Load(inputFileName.FullName);

                    foreach (string xpath in xpaths)
                    {
                        XmlNodeList iDList = xDoc.SelectNodes(xpath);

                        if (iDList != null && iDList.Count > 0)
                        {
                            foreach (XmlNode node in iDList)
                            {
                                string dummyValue = GetMaskedValue(node.InnerText);
                                node.InnerText = dummyValue;
                            }
                        }
                    }
                    string outputFileName = Path.Combine(outputFolderName, inputFileName.Name);
                    xDoc.Save(outputFileName);
                }
            }
            return true;
        }

        private string GetMaskedValue(string s)
        {
            string newString = Regex.Replace(s, "[.0-9]", "9");
            string newString1 = Regex.Replace(newString, "[.a-z,A-Z]", "Z");
            return newString1;
        }
    }
}
